package com.zyf.springcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyf.springcloud.dao.UserDao;
import com.zyf.springcloud.model.LoginData;
import com.zyf.springcloud.model.User;
import com.zyf.springcloud.model.selectObject;
import com.zyf.springcloud.result.Result;
import com.zyf.springcloud.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    UserDao userDao;

    //登录
    public Result login(LoginData data) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", data.getUsername());
        User user = userDao.selectOne(queryWrapper);
        System.out.println(user);
        if (user.getPassword().equals(data.getPassword())) {
            return Result.ok(data);
        }
        return Result.ok();
    }

    //查询所有
    public Result getAll() {
        return Result.ok(userDao.selectList(null));
    }

    //分页
    public Result getPage(int current, int limit, selectObject selectObject) {
        Page<User> page = new Page<>(current, limit);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(selectObject.getUsername())) {
            queryWrapper.eq("username", selectObject.getUsername());
        }
        IPage<User> iPage = userDao.selectPage(page, queryWrapper);
        return Result.ok(iPage);
    }

    //更新
    /*
    //emplopyeeDao.updateById(employee);//根据id进行更新，没有传值的属性就不会更新
        emplopyeeDao.updateAllColumnById(employee);//根据id进行更新，没传值的属性就更新为null
     */
    public Result updateUserById(Long id, LoginData data) {
        User user = new User();
        user.setId(id);
        user.setUsername(data.getUsername());
        user.setPassword(data.getPassword());
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        User user1 = userDao.selectOne(queryWrapper);
        user.setVersion(user1.getVersion());
        return Result.ok(userDao.updateById(user));
    }

    //删除
    public Result deleteUserByOne(Long id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return Result.ok(userDao.delete(queryWrapper));
    }

    //添加
    public Result addUser(User user) {
        System.out.println(user);
        userDao.insert(user);
        return Result.ok();
    }

    //查询By一个条件
    public Result selectByOne(QueryWrapper<User> queryWrapper) {
        return Result.ok(userDao.selectOne(queryWrapper));
    }
    //通过名字查询id
    public Long selectIdByUsername(String username){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        User user = userDao.selectOne(queryWrapper);
        return user.getId();
    }
}
